﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using QuanLyThietBi.BLL;
using QuanLyThietBi.DAL.Models;

namespace QuanLyThietBi
{
    public partial class CapPhatThietBi : Form
    {
        //private LoaiThietBiBO loaiThietBiBO;
        //private TinhTrangBO tinhtrangBO;
        private ThietBiBO thietbiBO;
        private int thietbiId = -1;
        //THIETBI thietbi;

        public CapPhatThietBi()
        {
            InitializeComponent();
        }

        public CapPhatThietBi(int thietbiID)
        {
            thietbiId = thietbiID;
            thietbiBO = new ThietBiBO();
            THIETBI tb = thietbiBO.GetThietBiById(thietbiId);

            InitializeComponent();

            //Bind All Data
            BindDataToComboBoxLoaiThietBi();
            cbxLoaiThietBi.SelectedValue = tb.loai_thiet_bi;
            BindDataToComboBoxThietBi(Convert.ToInt32(cbxLoaiThietBi.SelectedValue));//sao do du lieu vao ko dc?
            cbxThietBi.SelectedValue = thietbiId;
            BindDataToComboBoxPhongBan();
            BindDataToComboBoxNhanVien(Convert.ToInt32(cbxPhongBan.SelectedValue));
            lblMaTaiSan.Text = tb.ma_thiet_bi;
        }

        private void CapPhatThietBi_Load(object sender, EventArgs e)
        {
            if (thietbiId == -1)
            {
                BindDataToComboBoxLoaiThietBi();
                BindDataToComboBoxThietBi(Convert.ToInt32(cbxLoaiThietBi.SelectedValue));
                BindDataToComboBoxPhongBan();
                BindDataToComboBoxNhanVien(Convert.ToInt32(cbxPhongBan.SelectedValue));
            }
        }

        private void BindDataToComboBoxLoaiThietBi()
        {
            LoaiThietBiBO loaiThietBiBO = new LoaiThietBiBO();
            List<LOAITHIETBI> dsLoaiThietBi = loaiThietBiBO.GetAllLoaiThietBi();
            if (dsLoaiThietBi != null && dsLoaiThietBi.Count > 0)
            {
                cbxLoaiThietBi.DataSource = dsLoaiThietBi;
                cbxLoaiThietBi.DisplayMember = "loai_thiet_bi";
                cbxLoaiThietBi.ValueMember = "id";
            }
        }

        private void BindDataToComboBoxThietBi(int loaiThietBiId)
        {
            //cbxThietBi.Text = "";
            //cbxThietBi.Items.Clear();
            //cbxThietBi.SelectedIndex = -1;
            ThietBiBO thietbiBO = new ThietBiBO();
            List<THIETBI> dsThietBi = thietbiBO.GetThietBiChuaSuDung(loaiThietBiId);
            dsThietBi = dsThietBi.Where(t => t.tinh_trang == 1).ToList();
            if (dsThietBi != null && dsThietBi.Count > 0)
            {
                cbxThietBi.DataSource = dsThietBi;
                cbxThietBi.ValueMember = "id";
                cbxThietBi.DisplayMember = "ten_thiet_bi";
            }
            //else
            //{
            //    List<THIETBI> listThietBi = new List<THIETBI>() 
            //    { 
            //        new THIETBI() 
            //        { 
            //            ten_thiet_bi = string.Empty, id = -1 
            //        } 
            //    };
            //    cbxThietBi.DataSource = listThietBi;
            //    cbxThietBi.ValueMember = "id";
            //    cbxThietBi.DisplayMember = "ten_thiet_bi";
            //    lblMaTaiSan.Text = string.Empty;
            //}
        }

        private void BindDataToComboBoxPhongBan()
        {
            PhongBanBO phongbanBO = new PhongBanBO();
            List<PHONGBAN> dsPhongBan = phongbanBO.GetAllPhongBan();
            if (dsPhongBan != null && dsPhongBan.Count > 0)
            {
                cbxPhongBan.DataSource = dsPhongBan;
                cbxPhongBan.DisplayMember = "phong_ban";
                cbxPhongBan.ValueMember = "id";
            }
        }

        private void BindDataToComboBoxNhanVien(int phongBanId)
        {
            cbxNhanVien.SelectedIndex = -1;
            NhanVienBO nhanvienBO = new NhanVienBO();
            List<NHANVIEN> dsNhanVien = nhanvienBO.GetNhanVienByPhongBan(phongBanId);
            if (dsNhanVien != null && dsNhanVien.Count > 0)
            {
                cbxNhanVien.DataSource = dsNhanVien;
                cbxNhanVien.DisplayMember = "ten_day_du";
                cbxNhanVien.ValueMember = "id";
            }
            else
            {
                List<NHANVIEN> listNhanVien = new List<NHANVIEN>() { new NHANVIEN() { ten_day_du = string.Empty, id = -1 } };
                cbxNhanVien.DataSource = dsNhanVien;
                cbxNhanVien.DisplayMember = "ten_day_du";
                cbxNhanVien.ValueMember = "id";
            }
        }

        private void btnCap_Click(object sender, EventArgs e)
        {
            NhanVienThietBiBO nvtbBO = new NhanVienThietBiBO();
            NHANVIEN_THIETBI nvtb = new NHANVIEN_THIETBI()
            {
                thiet_bi = Convert.ToInt32(cbxThietBi.SelectedValue),
                nhan_vien = Convert.ToInt32(cbxNhanVien.SelectedValue),
                ngay = DateTime.Now
            };
            nvtbBO.SaveNhanVienThietBi(nvtb);

            PhongBanThietBiBO pbtbBO = new PhongBanThietBiBO();
            PHONGBAN_THIETBI pbtb = new PHONGBAN_THIETBI()
            {
                thiet_bi = Convert.ToInt32(cbxThietBi.SelectedValue),
                phong_ban = Convert.ToInt32(cbxNhanVien.SelectedValue),
                ngay = DateTime.Now
            };
            pbtbBO.SavePhongBanThietBi(pbtb);

            LichSuThietBiBO lstbBO = new LichSuThietBiBO();
            //string nhanviencap = "Cấp cho Nhân Viên : " + cbxNhanVien.Text;

            LICHSUTHIETBI lstb = new LICHSUTHIETBI()
            {
                id_thiet_bi = Convert.ToInt32(cbxThietBi.SelectedValue),
                ngay = DateTime.Now,
                noi_dung = "Cấp cho Nhân Viên : " + cbxNhanVien.Text

            };
            lstbBO.SaveLichSuThietBi(lstb);

            //Lấy lên thiết bị để update tình trang ==> đang sử dụng
            ThietBiBO tbbo = new ThietBiBO();
            THIETBI tb = tbbo.GetThietBiById(Convert.ToInt32(cbxThietBi.SelectedValue));//cbxThietBi.SelectedValue là giá trị dc chọn từ combobox
            tb.tinh_trang = 2; // 2 là đang sử dụng
            tbbo.UpdateThietBi(tb);

            MessageBox.Show("Cấp thiết bị thành công", "Thông Báo !!!");

            QuanLyThietBi ql = new QuanLyThietBi();
            ql.Show();
            this.Hide();
        }

        private void btnTroLai_Click(object sender, EventArgs e)
        {
            QuanLyThietBi ql = new QuanLyThietBi();
            ql.Show();
            this.Hide();
        }

        private void cbxLoaiThietBi_SelectionChangeCommitted(object sender, EventArgs e)
        {
            BindDataToComboBoxThietBi(Convert.ToInt32(cbxLoaiThietBi.SelectedValue));
        }

        private void cbxPhongBan_SelectionChangeCommitted(object sender, EventArgs e)
        {
            BindDataToComboBoxNhanVien(Convert.ToInt32(cbxPhongBan.SelectedValue));
        }

        private void cbxThietBi_SelectionChangeCommitted(object sender, EventArgs e)
        {
            ThietBiBO tbbo = new ThietBiBO();
            THIETBI tb = tbbo.GetThietBiById(Convert.ToInt32(cbxThietBi.SelectedValue));
            if (tb != null)
            {
                lblMaTaiSan.Text = tb.ma_thiet_bi;
            }
            else
            {
                lblMaTaiSan.Text = string.Empty;
            }
        }
    }
}
